package model

import (
	"github.com/jinzhu/gorm"
	"time"
)


const (
	SmsStatusUnused = 0
	SmsStatusUsed   = 1

	SmsUnSent = 0
	SmsSent = 1
	SmsSentFailed = 2
)

const (
	TopicSendSMS = "gsms_verify_send"
)



type SmsVerify struct {
	gorm.Model
	To         string `gorm:"Column:receiver;UNIQUE_INDEX:uix_sms_verify_string"`
	From       string `gorm:"Column:sender;UNIQUE_INDEX:uix_sms_verify_string"`
	Code       string `gorm:"Column:code"`
	VerifyType string `gorm:"Column:verify_type;UNIQUE_INDEX:uix_sms_verify_string"`
	Status     uint16 `gorm:"Column:status;UNIQUE_INDEX:uix_sms_verify_string"`
}


type SmsHistory struct {
	RequestID  string `gorm:"Column:request_id;primary_key"`
	To         string `gorm:"Column:to"`
	From       string `gorm:"Column:from"`
	Text       string `gorm:"Column:text"`
	VendorName string `gorm:"Column:vendor_name"`
	Status     uint16 `gorm:"Column:status"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (s *SmsVerify) TableName() string {
	return "sms_verify"
}

func (s *SmsHistory) TableName() string {
	return "sms_history"
}

func (s * SmsVerify) IsValid() bool {
	if s.Status == SmsStatusUnused && time.Now().Sub(s.Model.UpdatedAt) < 5 * time.Minute {
		return true
	}
	return false
}


